layui.use(['layer', 'form', 'table', 'admin', 'element', 'util'], function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var admin = layui.admin;
    var element = layui.element;
    var util = layui.util;
    var leadingOfficalPerson;
    var regionCatalogType;
    var Authorization;//请求接口用
    var microObjState = false;//判断基层索引是否创建完成
    var microIndexState = false;//判断基层单位索引是否创建完成
    var tipState = false;//判断核心索引是否创建完成

    //获取权限方法
    var userInfo = getAuthority();

    if (userInfo) {
        var access_token = localStorage.getExpire("access_token");
        var token_type = localStorage.getExpire("token_type");
        Authorization = token_type + " " + access_token;
        getMicroDataList();
        getMacroDataList();
    };
    //切换Tab
    element.on('tab(tabData)', function(data){
        let index = data.index;
        switch (index) {
            case 0:
                let objId = $('#microObj').val();
                getMicroDataItem(objId)
                break;
            case 1:
                let indexId = $('#microIndex').val();
                getMicroDataItem("name_"+indexId)
                break;
            case 2:
                let macroId = $('#macroIndex').val();
                getMicroDataItem(macroId)
                break;
            case 3:
                getMicroDataItem("tip")
        }
    });

    //查询基础数据表
    function getMicroDataList() {
        axios.get(searchUrl.microDataList,{
            headers:{
                'Authorization':Authorization
            }
        })
            .then(function (response) {
                if(response.status == 200 &&response.data.message == "success"){
                    var data = response.data.data;
                    // //渲染基层对象
                    var micro_str = template("template-micro-tmpl",{data});
                    document.getElementById("microObj").innerHTML = micro_str;
                    var microIndex_str = template("template-micro-tmpl",{data});
                    document.getElementById("microIndex").innerHTML = microIndex_str;
                    form.render("select")
                    form.on('select(microObj)', function(dataObj){
                        getMicroDataItem(dataObj.value)
                    })
                    form.on('select(microIndex)', function(dataObj){
                        getMicroDataItem("name_" + dataObj.value)
                    })
                    getMicroDataItem(data[0].id)
                }
            })
            .catch(function (error) {
                console.log(error);
            });
    }
    //查询综合数据表
    function getMacroDataList() {
        axios.get(searchUrl.regionLevel,{
            headers:{
                'Authorization':Authorization
            }
        })
            .then(function (response) {
                if(response.status == 200 &&response.data.message == "success"){
                    var data = response.data.data;
                    // //渲染基层对象
                    var macroIndex = template("template-macro-tmpl",{data});
                    document.getElementById("macroIndex").innerHTML = macroIndex;
                    form.render("select")
                    form.on('select(macroIndex)', function(dataObj){
                        getMicroDataItem(dataObj.value)
                    })
                }
            })
            .catch(function (error) {
                console.log(error);
            });
    }
    //根据ID获取数据ITEM
    function getMicroDataItem(id) {
        let req = {
            id:id
        }
        axios.get(searchUrl.microDataItem + id,{
            headers:{
                params: $.param(req),
                'Authorization':Authorization
            }
        })
            .then(function (response) {
                if(response.status == 200 &&response.data.message == "success"){
                    var data = response.data.data;
                    var str = template("index-info",{data});
                    document.getElementById("indexInfo").innerHTML = str;
                }
            })
            .catch(function (error) {
                console.log(error);
            });
    }
    //点击创建基层数据索引
    $('#creatObj').click(function () {
        let mitmid = $('#microObj').val();
        if(mitmid!=null){
            let text = $(this).html()
            $(this).attr('disabled', true)
            $(this).html('<i class="layui-icon layui-icon-loading layui-anim layui-anim-rotate layui-anim-loop"></i>')
            getCreatIndex(mitmid,"micro",$(this),text)
        }else{
            layer.msg("请先选择基层调查对象", {icon: 2});
            return;
        }
    })
    //基层单位名称索引
    $('#creatIndex').click(function () {
        let mitmid =  $('#microIndex').val();
        if(mitmid!=null){
            let text = $(this).html()
            $(this).attr('disabled', true)
            $(this).html('<i class="layui-icon layui-icon-loading layui-anim layui-anim-rotate layui-anim-loop"></i>')
            getCreatIndex(mitmid,"microname",$(this),text)
        }else{
            layer.msg("请先选择基层调查对象", {icon: 2});
            return;
        }
    })
    //创建核心索引
    $('#creatTip').click(function () {
        let text = $(this).html()
        $(this).attr('disabled', true)
        $(this).html('<i class="layui-icon layui-icon-loading layui-anim layui-anim-rotate layui-anim-loop"></i>')
        let mitmid =  "";
        getCreatIndex(mitmid,"tip",$(this),text)
    })
    //点击创建综合数据索引
    $('#creatMacroIndex').click(function () {
        let mitmid = $('#macroIndex').val();
        if(mitmid!=null){
            let text = $(this).html()
            $(this).attr('disabled', true)
            $(this).html('<i class="layui-icon layui-icon-loading layui-anim layui-anim-rotate layui-anim-loop"></i>')
            getCreatIndex(mitmid,"macro",$(this),text)
        }else{
            layer.msg("请先选择区划级别", {icon: 2});
            return;
        }
    })
    //创建基层数据索引
    function getCreatIndex(mitmid,model,node,text) {
        let url = "";
        if(model == "micro"){
            url = searchUrl.creatIndex + mitmid;
        }else if(model == "microname"){
            url = searchUrl.creatObj + mitmid;
            mitmid = "name_" + mitmid;
        }else if(model=="tip"){
            mitmid = "tip"
            url = searchUrl.creatTip;
        }else if(model=="macro"){
            url = searchUrl.creatMacroIndex + mitmid;
        }
        axios({
            method: 'post',
            url:url,
            headers:{
                'Authorization':Authorization
            }
        })
            .then(function (res) {
                if (res.data.data) {
                    getCreatState(mitmid,model,node,text)
                }else if(res.timestamp){
                    getCreatState(mitmid,model,node,text)
                }else {
                    node.removeAttr('disabled')
                    node.html(text)
                    layer.msg(res.data.message, {icon: 2});
                }
            })
            .catch(function (err) {
                console.log(err);
            })
    }
    function getCreatState(mitmid,model,node,text) {
        let creatTimer = setInterval(function () {
            let req = {
                status:model
            }
            axios.get(searchUrl.creatIndexState,{
                params:req,
                headers:{
                    'Authorization':Authorization
                }
            })
                .then(function (response) {
                    if(response.data.data){
                        clearInterval(creatTimer)
                        getMicroDataItem(mitmid)
                        node.removeAttr('disabled')
                        node.html(text)
                        layer.msg(response.data.message, {icon: 1});
                    }
                })
                .catch(function (error) {
                    console.log(error);
                });
        },1000)
    }

});